package com.example.saiaadmin.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.saiaadmin.common.GetCunrrentUser;
import com.example.saiaadmin.entry.EbSystemDictData;
import com.example.saiaadmin.entry.EbSystemLoginLog;
import com.example.saiaadmin.entry.EbSystemUser;
import com.example.saiaadmin.service.EbSystemLoginLogService;
import com.example.saiaadmin.utils.PageBean;
import com.example.saiaadmin.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 登录日志表Controller
 *
 *  @author  Vagrant
 * @since 2025/01/13
 */
@RestController
@RequestMapping("/eb-system-login-log")
public class EbSystemLoginLogController {

    @Autowired
    private EbSystemLoginLogService service;

    /**
     * 获取登录图表统计
     * @return
     */
    @GetMapping("/loginChart")
    public ResultData<Object> loginChart(){
        List<Map<String,Object>> list = service.loginChart();
        Map<String, Object> columnMap= new HashMap<>();
        List<String> listdate = list.stream().map(m->m.get("login_date").toString()).collect(Collectors.toList());
        List<String> listcount = list.stream().map(m->m.get("login_count").toString()).collect(Collectors.toList());
        /*Map<String, Object> listMap =list.stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (existing, replacement) -> existing
                ));*/
        columnMap.put("login_count",listcount);
        columnMap.put("login_date",listdate);
        return ResultData.success("查询成功",columnMap);
    }

    /**
     * 获取登录日志列表
     * @return
     */
    @GetMapping("/getLoginLogList")
    public ResultData<PageBean<EbSystemLoginLog>> getLoginLogList(Integer pageNum, Integer pageSize){
        if(null ==pageNum){
            pageNum=1;
        }
        if(null ==pageSize){
            pageSize=5;
        }
        Page<EbSystemLoginLog> pageinfo = new Page<>(pageNum,pageSize);
        GetCunrrentUser getCunrrentUser = new GetCunrrentUser();
        EbSystemUser systemUser=getCunrrentUser.getUser();
        LambdaQueryWrapper<EbSystemLoginLog> loginLogWrapper= new LambdaQueryWrapper<>();
        loginLogWrapper.eq(EbSystemLoginLog::getUsername,systemUser.getUsername());
        loginLogWrapper.orderByDesc(EbSystemLoginLog::getLoginTime);
         service.page(pageinfo,loginLogWrapper);

        PageBean<EbSystemLoginLog> pageBean =new PageBean<>();
        pageBean.setTotal(pageinfo.getTotal());
        pageBean.setData(pageinfo.getRecords());
        pageBean.setPer_page((int) pageinfo.getSize());
        pageBean.setLast_page((int) pageinfo.getPages());
        return ResultData.success("查询成功",pageBean);
    }
}
